package com.lanou.tipsy.fragment.singer.detail;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.singer.AlbumDetailAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.singer.singers_album.SAlbumData;
import com.lanou.tipsy.entity.singer.singers_album_detail.SAlbumsResultRoot;
import com.lanou.tipsy.entity.singer.singers_album_detail.SAlbumsResultSongList;
import com.lanou.tipsy.tools.volley.ResponseErrorListener;
import com.lanou.tipsy.tools.volley.VolleySingleton;

import java.util.ArrayList;

/**
 * Created by FengJun on 15/10/23.
 * 歌手-歌手详情-专辑详情
 */
public class SDetailAlbumDetailFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "SDetailAlbumDetailFragment";
    private RecyclerView recyclerView;
    private ArrayList<SAlbumsResultSongList> songLists;
    private VolleySingleton volleySingleton;
    private ResponseErrorListener errorListener;
    private SAlbumsResultSongList songList;
    private SAlbumData data;
    private AlbumDetailAdapter adapter;
    private StringRequest request;
    private Context context;
    private int albumId;
    private CollapsingToolbarLayout toolbar_layout;
    private Toolbar toolbar;
    private SimpleDraweeView draweeView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_singer_det_album_detail, null);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        data = (SAlbumData) bundle.get("albums");
        albumId = data.getAlbumId();
        //Log.d("SDetailAlbumDetailFragm", "songList.getAlbumId():" + data.getAlbumId());
        toolbar_layout.setTitle(data.getName());
        String url = data.getPicUrl();
        if (url != null && url != "") {
            Uri uri = Uri.parse(url);
            draweeView.setImageURI(uri);
        }
        analysis();
    }

    private void initView(View view) {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_album_detail);
        volleySingleton = VolleySingleton.getInstance();
        errorListener = new ResponseErrorListener();

        toolbar_layout = (CollapsingToolbarLayout) view.findViewById(R.id.ctl_album_detail);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar_album_detail);
        toolbar.setNavigationIcon(R.mipmap.btn_web_back_normal);
        toolbar.setNavigationOnClickListener(this);
        draweeView = (SimpleDraweeView) view.findViewById(R.id.draw_album_detail);
    }

    private void analysis() {
        String url = Constants.SINGERS_ALBUM_DETAIL_URL_START + albumId + Constants.SINGERS_ALBUM_DETAIL_URL_END;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                SAlbumsResultRoot root = JSON.parseObject(response, SAlbumsResultRoot.class);
                songLists = root.getData().getSongList();
                adapter = new AlbumDetailAdapter(context, songLists);
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(context));
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                click();
            }
        }, errorListener);
        volleySingleton.addQueue(request, TAG);
    }

    private void click() {
        adapter.setOnItemClickLitener(new AlbumDetailAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                songList = songLists.get(position);
                Toast.makeText(context,
                        position + " click, 名字是" + songList.getName(),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        getActivity().getSupportFragmentManager().popBackStack();
    }

    @Override
    public void onStop() {
        super.onStop();
        volleySingleton.removeRequest(TAG);
    }
}
